//
//  MyIntegral.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/21.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "MyIntegral.h"
#import "MyIntegralCell.h"
#import "PersonCenter.h"
#import "MineHomeViewController.h"
#import "OrderFormViewController.h"
#import "IntergralRecordListViewController.h"
#import "TaskModel.h"

@interface MyIntegral ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataDetailArray;
@property (nonatomic, strong) NSMutableArray *commonDetailArray;
@property (nonatomic, strong) NSMutableArray *accumulativeDetailArray;
@property (nonatomic, strong) NSMutableArray *dataBtnTitles;
@property (nonatomic, strong) UILabel *myScore;
@property (nonatomic, strong) UIButton *qianDaoBtn;
@property (nonatomic, strong) NSMutableArray *taskArr;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSArray *taskTypeArr;

@end

@implementation MyIntegral

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    self.title = @"我的积分";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"积分记录" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRecordList)];
    self.taskTypeArr = @[@"pro_wish", @"envy", @"comment", @"proed_wish", @"wish_success"];

}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    [self requestData];
}

- (NSMutableArray *)taskArr
{
    if (!_taskArr) {
        _taskArr = [NSMutableArray array];
    }
    return _taskArr;
}
//新手任务
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithObjects:@"完善资料",@"第一笔订单", @"全部完成新手任务", nil];
    }
    return _dataArray;
}
- (NSMutableArray *)dataDetailArray
{
    if (!_dataDetailArray) {
        _dataDetailArray = [NSMutableArray arrayWithObjects:@"修改头像添加地址+100",   @"第一笔订单+500", @"全部完成新手任务+200",nil];
    }
    return _dataDetailArray;
}
- (NSMutableArray *)dataBtnTitles
{
    if (!_dataBtnTitles) {
        _dataBtnTitles = [NSMutableArray arrayWithObjects:@"前往完善",@"前往购买",@"", nil];
        
    }
    return _dataBtnTitles;
}
//常规任务
- (NSMutableArray *)commonArray
{
    if (!_commonArray) {
        _commonArray = [NSMutableArray arrayWithObjects:@"订单",@"每日分享",@"晒单",@"募集心愿", @"参与心愿",nil];
    }
    return _commonArray;
}
- (NSMutableArray *)commonDetailArray

{
    if (!_commonDetailArray) {
        _commonDetailArray = [NSMutableArray arrayWithObjects:@"第二笔+300 第三笔+200 之后每次+100",@"+100计算 心愿墙+晒单 部分的分享",@"晒单一次+200（上不封顶）",@"发起1次心愿+100（每日上限2次）",@"参与1次心愿+100(每日上限20次)", nil];
    }
    return _commonDetailArray;
}
- (NSMutableArray *)commonBtnArr
{
    if (!_commonBtnArr) {
        _commonBtnArr = [NSMutableArray arrayWithObjects:@"前往购买",@"前往分享",@"前往晒单",@"前往募集",@"前往参与", nil];
    }
    return _commonBtnArr;
}
//获取积分
- (void)requestData
{
       NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, @"/user.do?cmd=integral"];
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]};
    [self requestByUrl:string dict:dic];
    
}

- (void)requestByUrl:(NSString *)url dict:(NSDictionary *)dict
{
    [NetworkHandle getInfoWithUrl:url dictionary:dict success:^(NSDictionary *dictionary) {
        if ([dictionary[@"code"] isEqualToString:@"200"]) {
            /*
             "is_perfect": "1",是否完善资料(0：未完成，1：完成)
             "first_sign": "1",第一次签到是否完成(0：未完成，1：完成)
             "first_order": "0",第一笔订单是否完成(0：未完成，1：完成)
             "noob_task": "0",是否全部完成新手任务
             "today_share": "0",今天分享了多少次
             "order_integral": "0",已完成订单多少个
             "shaidan_no": "0",晒单次数
             "make_wish": "0",今日发起心愿次数
             "join_wish": "0"今日参与心愿次数
             
             */

            self.dict = dictionary[@"data"];
            if ([self.dict[@"sign_exists"] integerValue] == 1) {
                [self.qianDaoBtn setBackgroundImage:[UIImage imageNamed:@"dlh"] forState:(UIControlStateNormal)];
            }
            self.myScore.text = [NSString stringWithFormat:@"%@积分", self.dict[@"total_integral"]];
            [self.tableView reloadData];
        } else {
            [Tools showAlert:dictionary[@"message"]];
        }
    }];

}

- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, kScreenWidth, 182 * kHeightScale)];
    self.myScore = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 160 * kWidthScale) / 2, 20, 160 * kWidthScale, 30)];
    self.myScore.font = [UIFont systemFontOfSize:30];
    self.myScore.textColor = [UIColor redColor];
    [aView addSubview:self.myScore];
    self.myScore.textAlignment = NSTextAlignmentCenter;
    
    self.qianDaoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.qianDaoBtn.frame = CGRectMake(154 * kWidthScale, self.myScore.bottom + 15, 66, 66);
    [self.qianDaoBtn setBackgroundImage:[UIImage imageNamed:@"dl"] forState:(UIControlStateNormal)];
    [aView addSubview:self.qianDaoBtn];
    [self.qianDaoBtn addTarget:self action:@selector(actionQianDao:) forControlEvents:(UIControlEventTouchUpInside)];
    self.tableView.tableHeaderView = aView;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyIntegralCell" bundle:nil] forCellReuseIdentifier:@"MyIntegralCell"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth, 34)];
        aView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 112, 14)];
        [aView addSubview:titleLb];
        if ([self.dict[@"noob_task"]integerValue]==1) {
            titleLb.text = @"常规任务";
        }else
        {
            titleLb.text = @"新手任务";
        }
        titleLb.textColor = kColors(218, 166, 46, 1);
        titleLb.font = [UIFont systemFontOfSize:15];
        
        return aView;

    }else if (section ==1)
    {
        UIView *aView1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth, 34)];
        aView1.backgroundColor = [UIColor whiteColor];
        UILabel *titleLb1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 112, 14)];
        [aView1 addSubview:titleLb1];
        titleLb1.text = @"常规任务";
        titleLb1.textColor = kColors(218, 166, 46, 1);
        titleLb1.font = [UIFont systemFontOfSize:15];
        
        return aView1;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([self.dict[@"noob_task"]integerValue]==1) {
        return 1;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if ([self.dict[@"noob_task"]integerValue]==1) {
             return self.commonArray.count;
        }else
        {return self.dataArray.count;
        }
    }else if (section == 1){
        return self.commonArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyIntegralCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIntegralCell" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {
         if ([self.dict[@"noob_task"]integerValue]==1)
         {
             cell.taskName.text = self.commonArray[indexPath.row];
             cell.taskDetail.text = self.commonDetailArray[indexPath.row];
             [cell.goToBtn setTitle:self.commonBtnArr[indexPath.row] forState:(UIControlStateNormal)];
             cell.goToBtn.backgroundColor = kColors(218, 166, 46, 1);
             cell.goToBtn.tag =1000+indexPath.row;
             cell.goToBtn.hidden =NO;
             
         }else{
        
        cell.taskName.text = self.dataArray[indexPath.row];
         cell.taskDetail.text = self.dataDetailArray[indexPath.row];
        [cell.goToBtn setTitle:self.dataBtnTitles[indexPath.row] forState:(UIControlStateNormal)];
        cell.goToBtn.backgroundColor = kColors(218, 166, 46, 1);
             
             if (indexPath.row ==0&&[self.dict[@"is_perfect"]integerValue]==1) {
                 [cell.goToBtn setTitle:@"已完成" forState:(UIControlStateNormal)];
                cell.goToBtn.backgroundColor = [UIColor grayColor];
                 cell.goToBtn.userInteractionEnabled =NO;
             }
             if (indexPath.row ==1&&[self.dict[@"first_order"]integerValue]==1)
             {
                 [cell.goToBtn setTitle:@"已完成" forState:(UIControlStateNormal)];
                 cell.backgroundColor = [UIColor grayColor];
                  cell.goToBtn.userInteractionEnabled =NO;
             }
             
        cell.goToBtn.tag = 1000+indexPath.row;
        if (indexPath.row ==2) {
            cell.goToBtn.backgroundColor = [UIColor whiteColor];
            cell.goToBtn.hidden =YES;
        }
             
        }
    }else if(indexPath.section ==1)
    {
        
        cell.taskName.text = self.commonArray[indexPath.row];
        cell.taskDetail.text = self.commonDetailArray[indexPath.row];
        [cell.goToBtn setTitle:self.commonBtnArr[indexPath.row] forState:(UIControlStateNormal)];
        cell.goToBtn.backgroundColor = kColors(218, 166, 46, 1);
        cell.goToBtn.tag =1000+self.dataArray.count+indexPath.row;
    }
    
    
    [cell.goToBtn addTarget:self action:@selector(goToBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (void)goToBtnAction:(UIButton *)button
{
    if ([self.dict[@"noob_task"]integerValue]==1)
    {
    switch (button.tag-1000) {
        case 0:
        {
            //前往购买
            self.tabBarController.selectedIndex = 0;
            [self.navigationController popToRootViewControllerAnimated:NO];
        }
            break;
        case 1:
        {
            //前往分享
            OrderFormViewController *orderVC = [[OrderFormViewController alloc] init];
            [self.navigationController pushViewController:orderVC animated:YES];
            orderVC.pushType = 0;
            
        }
            break;
        case 2:
        {
            //前往晒单
            OrderFormViewController *orderVC = [[OrderFormViewController alloc] init];
            [self.navigationController pushViewController:orderVC animated:YES];
            orderVC.pushType = 3;
            
        }
            break;
        case 3:
        {
            //募集心愿
            self.tabBarController.selectedIndex = 2;
            [self.navigationController popToRootViewControllerAnimated:NO];

            
        }
            break;
        case 4:
        {
            //参与心愿
            self.tabBarController.selectedIndex = 2;
            [self.navigationController popToRootViewControllerAnimated:NO];
            
        }
            break;
    
        default:
            break;
    }
    }else
    {
        switch (button.tag-1000) {
             case 0:
            {
               //前往完善资料
                PersonCenter *personCenter = [[PersonCenter alloc]init];
                [self.navigationController pushViewController:personCenter animated:YES];
            }
                break;
            case 1:
            {
                //前往购买
                self.tabBarController.selectedIndex = 0;
                [self.navigationController popToRootViewControllerAnimated:NO];
                
            }
                break;
            case 3:
            {
                //前往购买
                self.tabBarController.selectedIndex = 0;
                [self.navigationController popToRootViewControllerAnimated:NO];
            }
                break;
            case 4:
            {
                //前往分享
                OrderFormViewController *orderVC = [[OrderFormViewController alloc] init];
                [self.navigationController pushViewController:orderVC animated:YES];
                orderVC.pushType = 0;
                
            }
                break;
            case 5:
            {
                //前往晒单
                OrderFormViewController *orderVC = [[OrderFormViewController alloc] init];
                [self.navigationController pushViewController:orderVC animated:YES];
                orderVC.pushType = 3;
                
            }
                break;
            case 6:
            {
                //募集心愿
                self.tabBarController.selectedIndex = 2;
                [self.navigationController popToRootViewControllerAnimated:NO];
                
                
            }
                break;
            case 7:
            {
                //参与心愿
                self.tabBarController.selectedIndex = 2;
                [self.navigationController popToRootViewControllerAnimated:NO];
                
            }
                break;

            default:
                break;
        }
    }
    
    
}
- (void)actionQianDao:(UIButton *)button
{
    // /user.do?cmd=sign_integral&u=X
    NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, @"/user.do?cmd=sign_integral"];
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]};
    [NetworkHandle getInfoWithUrl:string dictionary:dic success:^(NSDictionary *dictionary) {
        if ([dictionary[@"code"] isEqualToString:@"200"]) {
            self.myScore.text = [NSString stringWithFormat:@"%@积分", dictionary[@"data"]];
            [button setBackgroundImage:[UIImage imageNamed:@"dlh"] forState:(UIControlStateNormal)];
            UIButton *btn = (UIButton *)[self.view viewWithTag:203];
            [btn setBackgroundColor:kColors(159, 159, 159, 1)];
        } else {
            [Tools showAlert:dictionary[@"message"]];
        }
    }];
}

- (void)actionRecordList
{
    IntergralRecordListViewController *VC = [[IntergralRecordListViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    VC.recordType = @"integral_record";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
